/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project.jpdftools.ui.managers;

import java.io.File;
import java.util.logging.Level;
import javax.swing.*;
import project.jpdftools.managers.LocalizationManager;
import project.jpdftools.managers.LoggerManager;
import project.jpdftools.managers.other.FileUtil;
import project.jpdftools.ui.MainMenu;
import project.jpdftools.ui.actionlisteners.ApplyIconAction;

/**
 *
 * @author Mido
 */
public class IconManager {

    private JMenu menu; // the menu that will be used to add the components in
    // icons radio buttons
    // must be static to be able to use them while initializing the program
    private static final JRadioButtonMenuItem rbmiIconISimpleIcons = new JRadioButtonMenuItem("iSimple Icons");
    private static final JRadioButtonMenuItem rbmiIconKidsIcons = new JRadioButtonMenuItem("kids Icons");
    private static final JRadioButtonMenuItem rbmiIconSoftScrapsIcons = new JRadioButtonMenuItem("Soft Scraps Icons");
    private static final JRadioButtonMenuItem rbmiIconMustHaveIcons = new JRadioButtonMenuItem("Must Have Icons");
    private static final ButtonGroup buttonGroup = new ButtonGroup();

    // constructor with parameters
    public IconManager(JMenu menu) {
        this.menu = menu;
        // intilaize the button group component
        buttonGroup.add(rbmiIconKidsIcons);
        buttonGroup.add(rbmiIconISimpleIcons);
        buttonGroup.add(rbmiIconSoftScrapsIcons);
        buttonGroup.add(rbmiIconMustHaveIcons);
    }

    /**
     * initializes previously saved icons of the program
     */
    public static void initializeIcons() {
        // check that the file already exists
        File icon_file = new File("Data/Icon.txt");
        if (icon_file.exists() == true) {
            // read icons from file
            String icons_folder = new FileUtil().readFromFile("Data/" + icon_file.getName());
            // make sure that the file is not empty
            if (icons_folder != null) {
                // apply saved icons
                applyIcons(icons_folder);
                // select the corresponding radio button menu item
                selectIconInIntializing(icons_folder);
                LoggerManager.getLogger().log(Level.INFO, "Selected icons \"{0}\"", icons_folder);
            } else {
                // tell the user that the file is empty
                JOptionPane.showMessageDialog(null, LocalizationManager.getCurrent_resource().getString("MAIN_MENU_ICON_EMPTY"),
                        LocalizationManager.getCurrent_resource().getString("EMPTY_TITLE"), 0);
                // select the corresponding radio button menu item
                selectIconInIntializing("Must Have Icons");
                // save default icons in the file for future uses
                new FileUtil().writeToFile("Data/Icon.txt", "Must Have Icons", false, false);
                LoggerManager.getLogger().log(Level.INFO, "Icons file is empty. Default icons selected \"Must Have Icons\"");
            }
        } else {
            // tell the user that the file is missing
            JOptionPane.showMessageDialog(null, LocalizationManager.getCurrent_resource().getString("MAIN_MENU_ICON_MISSING"),
                    LocalizationManager.getCurrent_resource().getString("MISSING_TITLE"), 0);
            // select the corresponding radio button menu item
            selectIconInIntializing("Must Have Icons");
            // save default icons in the file for future uses
            new FileUtil().writeToFile("Data/Icon.txt", "Must Have Icons", false, false);
            LoggerManager.getLogger().log(Level.INFO, "Icons file not found. Default icons selected \"Must Have Icons\"");
        }
    }

    /**
     * Selects the radioButtonMenuItem that corresponds to the specified string
     *
     * @param icons_folder the folder that contains the selected icons
     */
    private static void selectIconInIntializing(String icons_folder) {
        if (icons_folder.equals("iSimple System Icons") == true) {
            rbmiIconISimpleIcons.setSelected(true);
        } else if (icons_folder.equals("kids Icons") == true) {
            rbmiIconKidsIcons.setSelected(true);
        } else if (icons_folder.equals("Must Have Icons") == true) {
            rbmiIconMustHaveIcons.setSelected(true);
        } else if (icons_folder.equals("Soft Scraps Icons") == true) {
            rbmiIconSoftScrapsIcons.setSelected(true);
        }
    }

    /**
     * Applies the icon components in the UI
     */
    public void applyIconsInUI() {
        // add action listeners
        rbmiIconISimpleIcons.addActionListener(new ApplyIconAction("iSimple System Icons"));
        rbmiIconKidsIcons.addActionListener(new ApplyIconAction("kids Icons"));
        rbmiIconSoftScrapsIcons.addActionListener(new ApplyIconAction("Soft Scraps Icons"));
        rbmiIconMustHaveIcons.addActionListener(new ApplyIconAction("Must Have Icons"));

        // add radio button components to the menu
        menu.add(rbmiIconISimpleIcons);
        menu.add(rbmiIconKidsIcons);
        menu.add(rbmiIconSoftScrapsIcons);
        menu.add(rbmiIconMustHaveIcons);
    }

    /**
     * Applies the icons contained in the specified folder on the buttons
     *
     * @param folder_name
     */
    public static void applyIcons(String folder_name) {
        File[] icons = new File("Data/Icons/" + folder_name).listFiles();
        JButton[] buttons = new JButton[]{MainMenu.getInstance().getBt_new_pdf(), MainMenu.getInstance().getBt_load_file(),
            MainMenu.getInstance().getBt_save_file(), MainMenu.getInstance().getBt_merge_files(),
            MainMenu.getInstance().getBt_split_file(), MainMenu.getInstance().getBt_convert(), MainMenu.getInstance().getBt_merge_images(),
            MainMenu.getInstance().getBt_add_text(), MainMenu.getInstance().getBt_add_image(), MainMenu.getInstance().getBt_export_pdf(),
            MainMenu.getInstance().getBt_add_table()};
        // loop on all the buttons and apply the imageicons
        for (int i = 0; i < 11; i++) {
            buttons[i].setIcon(new ImageIcon("Data/Icons/" + folder_name + "/" + icons[i].getName()));
        }
    }
}
